{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Validation 和 Cross Validation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn import datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "digits = datasets.load_digits()\n",
    "X = digits.data\n",
    "y = digits.target"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 测试train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=666)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Best K = 3\nBest P = 4\nBest Score = 0.9860917941585535\n"
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "\n",
    "# 对k(邻居)，p(距离的计算方式)进行调参\n",
    "best_k, best_p, best_score = 0, 0, 0\n",
    "for k in range(2, 11):\n",
    "    for p in range(1, 6):\n",
    "        knn_clf = KNeighborsClassifier(weights=\"distance\", n_neighbors=k, p=p)\n",
    "        knn_clf.fit(X_train, y_train)\n",
    "        score = knn_clf.score(X_test, y_test)\n",
    "        if score > best_score:\n",
    "            best_k, best_p, best_score = k, p, score\n",
    "            \n",
    "print(\"Best K =\", best_k)\n",
    "print(\"Best P =\", best_p)\n",
    "print(\"Best Score =\", best_score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用交叉验证"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([0.99537037, 0.98148148, 0.97685185, 0.97674419, 0.97209302])"
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "from sklearn.model_selection import cross_val_score\n",
    "\n",
    "knn_clf = KNeighborsClassifier()\n",
    "# 默认就是对X_train分成5份进行验证\n",
    "cross_val_score(knn_clf, X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Best K = 2\nBest P = 2\nBest Score = 0.9851507321274763\n"
    }
   ],
   "source": [
    "best_k, best_p, best_score = 0, 0, 0\n",
    "for k in range(2, 11):\n",
    "    for p in range(1, 6):\n",
    "        knn_clf = KNeighborsClassifier(weights=\"distance\", n_neighbors=k, p=p)\n",
    "        # 直接使用CVscore计算即可\n",
    "        scores = cross_val_score(knn_clf, X_train, y_train)\n",
    "        score = np.mean(scores)\n",
    "        if score > best_score:\n",
    "            best_k, best_p, best_score = k, p, score\n",
    "            \n",
    "# 使用CV得到的参数更加可信。因为手动的train-test-split的有可能只是对test存在过拟合现象\n",
    "# 得到的best_score并不是最终评价该模型的分数\n",
    "# 拿到这组K和P之后得到的knnclassifier求出score即可\n",
    "print(\"Best K =\", best_k)\n",
    "print(\"Best P =\", best_p)\n",
    "print(\"Best Score =\", best_score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.980528511821975"
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "best_knn_clf = KNeighborsClassifier(weights=\"distance\", n_neighbors=2, p=2)\n",
    "best_knn_clf.fit(X_train, y_train)\n",
    "# test是在网格搜索中完全没有使用的，所以得到的结果更加可信\n",
    "best_knn_clf.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 回顾网格搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Fitting 5 folds for each of 45 candidates, totalling 225 fits\n[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.\n[Parallel(n_jobs=1)]: Done 225 out of 225 | elapsed:   34.6s finished\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "GridSearchCV(cv=None, error_score=nan,\n             estimator=KNeighborsClassifier(algorithm='auto', leaf_size=30,\n                                            metric='minkowski',\n                                            metric_params=None, n_jobs=None,\n                                            n_neighbors=10, p=5,\n                                            weights='distance'),\n             iid='deprecated', n_jobs=None,\n             param_grid=[{'n_neighbors': [2, 3, 4, 5, 6, 7, 8, 9, 10],\n                          'p': [1, 2, 3, 4, 5], 'weights': ['distance']}],\n             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n             scoring=None, verbose=1)"
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "param_grid = [\n",
    "    {\n",
    "        'weights': ['distance'],\n",
    "        'n_neighbors': [i for i in range(2, 11)], \n",
    "        'p': [i for i in range(1, 6)]\n",
    "    }\n",
    "]\n",
    "\n",
    "# 网格搜索的GridSearchCV就是使用CV进行搜索的\n",
    "grid_search = GridSearchCV(knn_clf, param_grid, verbose=1)\n",
    "grid_search.fit(X_train, y_train)\n",
    "\n",
    "# 225 = 5*45"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.9851507321274763"
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "grid_search.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "{'n_neighbors': 2, 'p': 2, 'weights': 'distance'}"
     },
     "metadata": {},
     "execution_count": 15
    }
   ],
   "source": [
    "grid_search.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.980528511821975"
     },
     "metadata": {},
     "execution_count": 18
    }
   ],
   "source": [
    "best_knn_clf = grid_search.best_estimator_\n",
    "best_knn_clf.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### cv参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([0.99074074, 1.        , 0.98148148, 0.9537037 , 0.99074074,\n       0.96296296, 0.94444444, 0.98148148, 0.98130841, 0.98130841])"
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "source": [
    "cross_val_score(knn_clf, X_train, y_train, cv=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true,
    "tags": [
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend",
     "outputPrepend"
    ]
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "neighbors=8, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=8, p=2, weights=distance ............................\n[CV] ............. n_neighbors=8, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=8, p=2, weights=distance ............................\n[CV] ............. n_neighbors=8, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=8, p=2, weights=distance ............................\n[CV] ............. n_neighbors=8, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=8, p=2, weights=distance ............................\n[CV] ............. n_neighbors=8, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=8, p=2, weights=distance ............................\n[CV] ............. n_neighbors=8, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=8, p=2, weights=distance ............................\n[CV] ............. n_neighbors=8, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=8, p=2, weights=distance ............................\n[CV] ............. n_neighbors=8, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=8, p=2, weights=distance ............................\n[CV] ............. n_neighbors=8, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=8, p=2, weights=distance ............................\n[CV] ............. n_neighbors=8, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=8, p=2, weights=distance ............................\n[CV] ............. n_neighbors=8, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=8, p=3, weights=distance ............................\n[CV] ............. n_neighbors=8, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=3, weights=distance ............................\n[CV] ............. n_neighbors=8, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=3, weights=distance ............................\n[CV] ............. n_neighbors=8, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=3, weights=distance ............................\n[CV] ............. n_neighbors=8, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=3, weights=distance ............................\n[CV] ............. n_neighbors=8, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=3, weights=distance ............................\n[CV] ............. n_neighbors=8, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=3, weights=distance ............................\n[CV] ............. n_neighbors=8, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=3, weights=distance ............................\n[CV] ............. n_neighbors=8, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=3, weights=distance ............................\n[CV] ............. n_neighbors=8, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=3, weights=distance ............................\n[CV] ............. n_neighbors=8, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=4, weights=distance ............................\n[CV] ............. n_neighbors=8, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=4, weights=distance ............................\n[CV] ............. n_neighbors=8, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=4, weights=distance ............................\n[CV] ............. n_neighbors=8, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=4, weights=distance ............................\n[CV] ............. n_neighbors=8, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=4, weights=distance ............................\n[CV] ............. n_neighbors=8, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=4, weights=distance ............................\n[CV] ............. n_neighbors=8, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=4, weights=distance ............................\n[CV] ............. n_neighbors=8, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=4, weights=distance ............................\n[CV] ............. n_neighbors=8, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=4, weights=distance ............................\n[CV] ............. n_neighbors=8, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=4, weights=distance ............................\n[CV] ............. n_neighbors=8, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=5, weights=distance ............................\n[CV] ............. n_neighbors=8, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=5, weights=distance ............................\n[CV] ............. n_neighbors=8, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=5, weights=distance ............................\n[CV] ............. n_neighbors=8, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=5, weights=distance ............................\n[CV] ............. n_neighbors=8, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=5, weights=distance ............................\n[CV] ............. n_neighbors=8, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=5, weights=distance ............................\n[CV] ............. n_neighbors=8, p=5, weights=distance, total=   0.1s\n[CV] n_neighbors=8, p=5, weights=distance ............................\n[CV] ............. n_neighbors=8, p=5, weights=distance, total=   0.1s\n[CV] n_neighbors=8, p=5, weights=distance ............................\n[CV] ............. n_neighbors=8, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=8, p=5, weights=distance ............................\n[CV] ............. n_neighbors=8, p=5, weights=distance, total=   0.1s\n[CV] n_neighbors=8, p=5, weights=distance ............................\n[CV] ............. n_neighbors=8, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=1, weights=distance ............................\n[CV] ............. n_neighbors=9, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=1, weights=distance ............................\n[CV] ............. n_neighbors=9, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=1, weights=distance ............................\n[CV] ............. n_neighbors=9, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=1, weights=distance ............................\n[CV] ............. n_neighbors=9, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=1, weights=distance ............................\n[CV] ............. n_neighbors=9, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=1, weights=distance ............................\n[CV] ............. n_neighbors=9, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=1, weights=distance ............................\n[CV] ............. n_neighbors=9, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=1, weights=distance ............................\n[CV] ............. n_neighbors=9, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=1, weights=distance ............................\n[CV] ............. n_neighbors=9, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=1, weights=distance ............................\n[CV] ............. n_neighbors=9, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=2, weights=distance ............................\n[CV] ............. n_neighbors=9, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=2, weights=distance ............................\n[CV] ............. n_neighbors=9, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=2, weights=distance ............................\n[CV] ............. n_neighbors=9, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=2, weights=distance ............................\n[CV] ............. n_neighbors=9, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=2, weights=distance ............................\n[CV] ............. n_neighbors=9, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=2, weights=distance ............................\n[CV] ............. n_neighbors=9, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=2, weights=distance ............................\n[CV] ............. n_neighbors=9, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=2, weights=distance ............................\n[CV] ............. n_neighbors=9, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=2, weights=distance ............................\n[CV] ............. n_neighbors=9, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=2, weights=distance ............................\n[CV] ............. n_neighbors=9, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=9, p=3, weights=distance ............................\n[CV] ............. n_neighbors=9, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=3, weights=distance ............................\n[CV] ............. n_neighbors=9, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=3, weights=distance ............................\n[CV] ............. n_neighbors=9, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=3, weights=distance ............................\n[CV] ............. n_neighbors=9, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=3, weights=distance ............................\n[CV] ............. n_neighbors=9, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=3, weights=distance ............................\n[CV] ............. n_neighbors=9, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=3, weights=distance ............................\n[CV] ............. n_neighbors=9, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=3, weights=distance ............................\n[CV] ............. n_neighbors=9, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=3, weights=distance ............................\n[CV] ............. n_neighbors=9, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=3, weights=distance ............................\n[CV] ............. n_neighbors=9, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=4, weights=distance ............................\n[CV] ............. n_neighbors=9, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=4, weights=distance ............................\n[CV] ............. n_neighbors=9, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=4, weights=distance ............................\n[CV] ............. n_neighbors=9, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=4, weights=distance ............................\n[CV] ............. n_neighbors=9, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=4, weights=distance ............................\n[CV] ............. n_neighbors=9, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=4, weights=distance ............................\n[CV] ............. n_neighbors=9, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=4, weights=distance ............................\n[CV] ............. n_neighbors=9, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=4, weights=distance ............................\n[CV] ............. n_neighbors=9, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=4, weights=distance ............................\n[CV] ............. n_neighbors=9, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=4, weights=distance ............................\n[CV] ............. n_neighbors=9, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=5, weights=distance ............................\n[CV] ............. n_neighbors=9, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=5, weights=distance ............................\n[CV] ............. n_neighbors=9, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=5, weights=distance ............................\n[CV] ............. n_neighbors=9, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=5, weights=distance ............................\n[CV] ............. n_neighbors=9, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=5, weights=distance ............................\n[CV] ............. n_neighbors=9, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=5, weights=distance ............................\n[CV] ............. n_neighbors=9, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=5, weights=distance ............................\n[CV] ............. n_neighbors=9, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=5, weights=distance ............................\n[CV] ............. n_neighbors=9, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=5, weights=distance ............................\n[CV] ............. n_neighbors=9, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=9, p=5, weights=distance ............................\n[CV] ............. n_neighbors=9, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=1, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=1, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=1, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=1, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=1, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=1, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=1, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=1, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=1, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=1, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=1, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=2, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=2, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=2, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=2, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=2, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=2, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=2, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=2, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=2, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=2, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=2, weights=distance, total=   0.0s\n[CV] n_neighbors=10, p=3, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=3, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=3, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=3, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=3, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=3, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=3, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=3, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=3, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=3, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=3, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=4, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=4, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=4, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=4, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=4, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=4, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=4, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=4, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=4, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=4, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=4, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=5, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=5, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=5, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=5, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=5, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=5, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=5, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=5, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=5, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=5, weights=distance, total=   0.2s\n[CV] n_neighbors=10, p=5, weights=distance ...........................\n[CV] ............ n_neighbors=10, p=5, weights=distance, total=   0.2s\n[Parallel(n_jobs=1)]: Done 450 out of 450 | elapsed:   45.4s finished\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "GridSearchCV(cv=10, error_score=nan,\n             estimator=KNeighborsClassifier(algorithm='auto', leaf_size=30,\n                                            metric='minkowski',\n                                            metric_params=None, n_jobs=None,\n                                            n_neighbors=10, p=5,\n                                            weights='distance'),\n             iid='deprecated', n_jobs=None,\n             param_grid=[{'n_neighbors': [2, 3, 4, 5, 6, 7, 8, 9, 10],\n                          'p': [1, 2, 3, 4, 5], 'weights': ['distance']}],\n             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n             scoring=None, verbose=2)"
     },
     "metadata": {},
     "execution_count": 25
    }
   ],
   "source": [
    "grid_search = GridSearchCV(knn_clf, param_grid, verbose=1, cv=10)\n",
    "grid_search.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.8 64-bit ('anaconda': conda)",
   "language": "python",
   "name": "python36864bitanacondaconda085db2e8b14649f4b32196068f7124e9"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7-final"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}